//SUMMARY:  This do file cleans and saves the survey data for Ballard-Rosa, Malik, Rickard & Scheve
clear all
set more off

* Set directory
cd "C:\Users\cambr\Dropbox\Conjoint Taxation\UK study\AuthoritarianDraft\Submissions\CPS\Replication materials\Data\"

import delimited "BMRS original survey.csv", clear

//Clean/rename several covariates of interest:
rename q15a nationalPride
recode nationalPride (1=4) (2=3) (3=2) (4=1)
label define proudLabel 1 "Not at all proud" 2 "Not very proud" 3 "Quite proud" 4 "Very proud"
label val nationalPride proudLabel

rename q23_2 unemployment5yrs
recode unemployment5yrs (1=1) (2=0)

rename q24 employmentStatus
gen unemployedCurrently = cond(employmentStatus == 4, 1, 0) if employmentStatus != .

rename q36 parentsFinancial
gen betterThanParents = cond(parentsFinancial == 1, 1, 0) if parentsFinancial != .

rename q43 partyPref
gen ukipSupporter = cond(partyPref == 6, 1, 0) if partyPref != .

rename q51 racialResentment
gen high_resentment = cond(racialResentment == 1 | racialResentment == 2, 1, 0) if racialResentment != .

rename q52al1 youngPeople
rename q52al2 deathPenalty
rename q52al3 obeyAuthority
rename q52al4 censorship
rename q52al5 stifferSentences

gen authoritarianTotal = (youngPeople + deathPenalty + obeyAuthority + censorship + stifferSentences) / 5
gen isAuthoritarian = cond(authoritarianTotal > 3.5, 1, 0) if authoritarianTotal != .

rename q54 sameCustoms

rename q56 friendImmigrants
gen noFriends = cond(friendImmigrants == 3, 1, 0) if friendImmigrants != .

rename q58 numImmigrants
label define reducedLabel 1 "Increased a lot" 2 "Increased a little" 3 "Remain the same" 4 "Reduced a little" 5 "Reduced a lot"
label val numImmigrants reducedLabel
gen reduceImmigration = cond(numImmigrants == 4 | numImmigrants == 5, 1, 0) if numImmigrants != .

rename q59 tradeOnUK
label define hurtLabel 1 "Helped a lot" 2 "Helped some" 3 "Neither helped nor hurt" 4 "Hurt some" 5 "Hurt a lot"
label val tradeOnUK hurtLabel
rename q60 tradeOnCommunity
label val tradeOnCommunity hurtLabel

gen tradeBenefitUK = cond(tradeOnUK == 1 | tradeOnUK == 2, 1, 0) if tradeOnUK != .
gen tradeBenefitCommunity = cond(tradeOnCommunity == 1 | tradeOnCommunity == 2, 1, 0) if tradeOnCommunity != .
gen communityVsUK = tradeBenefitCommunity - tradeBenefitUK

rename q61 reduceTrade
recode reduceTrade (6=.)
label define reduceLabel 1 "Expanded greatly" 2 "Expanded somewhat" 3 "Kept at its current level" 4 "Reduced somewhat" 5 "Reduced greatly"
label val reduceTrade reduceLabel
gen expandTradeDummy = cond(reduceTrade == 1 | reduceTrade == 2, 1, 0) if reduceTrade != .

rename q64 brexitDidVote
rename q65 brexitHowVote
gen votedLeave = cond(brexitHowVote == 2, 1, 0) if brexitHowVote != .
replace votedLeave = . if brexitHowVote == 9

rename q69 likelihoodLivingExpenses
gen livingExpenses12mo = cond(likelihoodLivingExpenses == 4 | likelihoodLivingExpenses == 5, 1, 0) if likelihoodLivingExpenses != .
gen secureLivingExp = cond(likelihoodLivingExpenses == 1 | likelihoodLivingExpenses == 2, 1, 0) if likelihoodLivingExpenses != .
gen veryInsecureLiving = cond(likelihoodLivingExpenses == 5, 1, 0) if likelihoodLivingExpenses != .

rename q70 likelihoodLoseJob
gen loseJob12mo = cond(likelihoodLoseJob == 4 | likelihoodLoseJob == 5, 1, 0) if likelihoodLoseJob != .

rename q84 publicOfficials
label define officialsLabel 1 "Agree strongly" 2 "Agree somewhat" 3 "Neither agree nor disagree" 4 "Disagree somewhat" 5 "Disagree strongly"
label val publicOfficials officialsLabel
label var publicOfficials "Public officials don't care about me"
rename q87 pastBetter
rename q88 childrenBetter

gen influenceTotal = (publicOfficials + pastBetter + childrenBetter) / 3
gen highInfluence = cond(influenceTotal > 2.7, 1, 0) if influenceTotal != .

gen age = 2017 - birthyear

gen female = cond(profile_gender == 2, 1, 0) if profile_gender != .

gen any_unemployment = unemployment5yrs + unemployedCurrently + loseJob12mo
replace any_unemployment = 1 if any_unemployment > 1

rename profile_education_level educ
replace educ = . if educ == 19 | educ == 20
gen universityEduc = cond(educ >= 15, 1, 0) if educ != .

rename profile_gross_household hh_inc
replace hh_inc = . if hh_inc == 16 | hh_inc == 17 | hh_inc == 98 //"Prefer not to answer" "Don't know" "Skipped"
gen high_income = cond(hh_inc >= 7, 1, 0) if hh_inc != .
gen inc_qt1 = cond(hh_inc <= 3, 1, 0) if hh_inc != .
gen inc_qt2 = cond(hh_inc >= 4 & hh_inc <= 6, 1, 0) if hh_inc != .
gen inc_qt3 = cond(hh_inc >= 7 & hh_inc <= 9, 1, 0) if hh_inc != .
gen inc_qt4 = cond(hh_inc >= 10 & hh_inc <= 15, 1, 0) if hh_inc != .

//Need to recode industry data:
gen section = "."
rename q25 industry

replace section = "A" if industry == 1
replace section = "B" if industry == 2
replace section = "C" if industry == 3 
replace section = "D" if industry == 4
replace section = "E" if industry == 5 //NOTE:  have none of these
replace section = "F" if industry == 6
replace section = "G" if industry == 7
replace section = "H" if industry == 8
replace section = "I" if industry == 9
replace section = "J" if industry == 10
replace section = "K" if industry == 11
replace section = "L" if industry == 12
replace section = "M" if industry == 13
replace section = "N" if industry == 14
replace section = "O" if industry == 15
replace section = "P" if industry == 16
replace section = "Q" if industry == 17
replace section = "R" if industry == 18
replace section = "S" if industry == 19
replace section = "T" if industry == 20

rename q11_a respectVindep
recode respectVindep (1=0) (2=1)
rename q11_b mannersVcuriosity
recode mannersVcuriosity (1=0) (2=1)
rename q11_c obedienceVself
recode obedienceVself (1=1) (2=0)
rename q11_d behavedVconsiderate
recode behavedVconsiderate (1=0) (2=1)

gen childRearingAuth = (respectVindep + mannersVcuriosity + obedienceVself + behavedVconsiderate)/4

//Aggression
rename q12_m forceNecessaryGroupsThreat
rename q12_n forceNecessaryIndivThreat
rename q12_o policeAvoidViolenceREV
recode policeAvoidViolence (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_p peopleAvoidViolenceREV
recode peopleAvoidViolence (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_q forceIsWrongREV
recode forceIsWrong (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_r strongPunishmentsNecessary

gen avgAggression = (forceNecessaryGroups + forceNecessaryIndiv + policeAvoidViolenceREV + peopleAvoidViolenceREV + forceIsWrongREV + strongPunishmentsNecessary) / 6
alpha forceNecessaryGroupsThreat forceNecessaryIndivThreat policeAvoidViolenceREV peopleAvoidViolenceREV forceIsWrongREV strongPunishmentsNecessary, item

//Submission
rename q12_a believeLeaders
rename q12_b leadersKnowBest
rename q12_c criticizeAuthoritiesREV
recode criticizeAuthorities (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_d authoritiesTruthful
rename q12_e skepticalOfAuthoritiesREV
recode skepticalOfAuthorities (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_f questioningHealthyREV
recode questioningHealthy (1=5) (2=4) (3=3) (4=2) (5=1)

gen avgSubmission = (believeLeaders + leadersKnowBest + criticizeAuthoritiesREV + authoritiesTruthful + skepticalOfAuthoritiesREV + questioningHealthyREV) / 6
alpha believeLeaders leadersKnowBest criticizeAuthoritiesREV authoritiesTruthful skepticalOfAuthoritiesREV questioningHealthyREV, item

//Conventionalism
rename q12_g tooMuchTraditionREV
recode tooMuchTradition (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_h traditionsFoundationSociety
rename q12_i followSocialTraditions
rename q12_j traditionsInterfereProgressREV
recode traditionsInterfereProgress (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_k challengeSocialTraditionsREV
recode challengeSocialTraditions (1=5) (2=4) (3=3) (4=2) (5=1)
rename q12_l respectNorms

gen avgConventionalism = (tooMuchTraditionREV + traditionsFoundationSociety + followSocialTraditions + traditionsInterfereProgressREV + challengeSocialTraditionsREV + respectNorms) / 6
alpha tooMuchTraditionREV traditionsFoundationSociety followSocialTraditions traditionsInterfereProgressREV challengeSocialTraditionsREV respectNorms, item

gen asc = (avgAggression + avgSubmission + avgConventionalism) / 3


//Self-esteem:
rename q19_1 satisfiedWithSelfREV
recode satisfiedWithSelfREV (1=4) (2=3) (3=2) (4=1)
label define reverseAgreeLabel 1 "Strongly disagree" 2 "Disagree" 3 "Agree" 4 "Strongly agree"
label val satisfiedWithSelfREV reverseAgreeLabel
rename q19_2 noGoodAtAll
label define agreeLabel 1 "Strongly agree" 2 "Agree" 3 "Disagree" 4 "Strongly disagree"
label val noGoodAtAll agreeLabel
rename q19_3 goodQualitiesREV
recode goodQualitiesREV (1=4) (2=3) (3=2) (4=1)
label val goodQualitiesREV reverseAgreeLabel
rename q19_4 doThingsWellREV
recode doThingsWellREV (1=4) (2=3) (3=2) (4=1)
label val doThingsWellREV reverseAgreeLabel
rename q19_5 notMuchProudOf
label val notMuchProudOf agreeLabel
rename q19_6 useless
label val useless agreeLabel
rename q19_7 personOfWorthREV
recode personOfWorthREV (1=4) (2=3) (3=2) (4=1)
label val personOfWorthREV reverseAgreeLabel
rename q19_8 wishMoreRespect
label val wishMoreRespect agreeLabel
rename q19_9 amFailure
label val amFailure agreeLabel
rename q19_10 positiveAttitudeREV
recode positiveAttitudeREV (1=4) (2=3) (3=2) (4=1)
label val positiveAttitudeREV reverseAgreeLabel

gen rosenburgSelfEsteem = satisfiedWithSelfREV + noGoodAtAll + goodQualitiesREV + doThingsWellREV + notMuchProudOf + useless + personOfWorthREV + wishMoreRespect + amFailure + positiveAttitudeREV
gen rosenburgNegativeWording = noGoodAtAll + notMuchProudOf + useless + wishMoreRespect + amFailure

factor satisfiedWithSelfREV - positiveAttitudeREV, pcf
rotate
predict factor1 factor2
rename factor1 selfSatisfactionFA
rename factor2 selfSatisfactionREV_FA

//Anxiety:
rename q20_1 nervousAnxious
rename q20_2 notStopWorrying
rename q20_3 worryTooMuch
rename q20_4 troubleRelaxing
rename q20_5 hardSitStill
rename q20_6 easilyAnnoyed
rename q20_7 awfulMightHappen
rename q20_followup anxietyImpact
recode anxietyImpact (8=.) (9=.)
replace anxietyImpact = 0 if anxietyImpact == .


gen gad7 = nervousAnxious + notStopWorrying + worryTooMuch + troubleRelaxing + hardSitStill + easilyAnnoyed + awfulMightHappen

//Other variables:
gen isMarried = cond(profile_marital_stat == 1, 1, 0)
gen isReligious = cond(profile_religion != 1 & profile_religion != 16, 1, 0)
rename q19 numChildren
gen age3150=0
replace age3150=1 if age>30 & age<=50
gen age5170=0
replace age5170=1 if age>50 & age<=70
gen agegt70=0
replace agegt70=1 if age>70 & age~=.
rename q27 personalIncome
gen ownIncLower = cond(personalInc == 1, 1, 0)
gen ownIncMiddle = cond(personalInc == 2, 1, 0)
gen ownIncUpper = cond(personalInc > 2, 1, 0)

gen higherCert = cond(educ >= 11 & educ < 15, 1, 0) if educ != .
gen lowIncome = cond(hh_inc <= 5, 1, 0) if hh_inc != .

gen tradeExposed = cond(industry == 3 | industry == 7, 1, 0) if industry != .

rename q29_1 govtPension
recode govtPension (2=0) (1=1)

rename q29_7 noneGovtAssist
gen receivedGovtAssist = cond(noneGovtAssist == 1, 1, 0)

rename q30_2 redColor
rename q30_3 greenColor

gen attentionCheck = cond(redColor == 1 & greenColor == 1, 1, 0)

rename q39 rightwing
gen polarization = abs(rightwing-5)

rename q63 foreignInvest
label define foreignInvestLabel 1 "Encourage additional foreign investment" 2 "Restrict additional foreign investment" 3 "Don't know"
label val foreignInvest foreignInvestLabel
gen encourageForeignInvestment = cond(foreignInvest == 1, 1, 0) if foreignInvest != .

rename q34 unempVSinflation
gen unemp_inflation_correct = cond(unempVSinflation == 1, 1, 0)

rename q41 partyVotedFor
gen conservativeVoter = cond(partyVotedFor == 1, 1, 0) if partyVotedFor != .
gen labourVoter = cond(partyVotedFor == 2, 1, 0) if partyVotedFor != .
gen libdemVoter = cond(partyVotedFor == 3, 1, 0) if partyVotedFor != .
gen snpVoter = cond(partyVotedFor == 5, 1, 0) if partyVotedFor != .
gen ukipVoter = cond(partyVotedFor == 6, 1, 0) if partyVotedFor != .

destring profile_ethnicity_pdl, ignore("") replace
gen isWhite = cond(profile_ethnicity_pdl == 1, 1, 0) if profile_ethnicity_pdl != .

rename q79 religImport
recode religImport (1=4) (2=3) (3=2) (4=1)
label define importantLabel 1 "Not at all important" 2 "Not too important" 3 "Somewhat important" 4 "Very important"
label val religImport importantLabel

rename q10_a class1st
gen middleClass = cond(class1st == 1, 1, 0)
gen workingClass = cond(class1st == 2, 1, 0)
rename q10_b class2nd
replace middleClass = 1 if class2nd == 1
replace workingClass = 1 if class2nd == 2

rename q14a communityEcon10yrs
label def gotWorseLabel 1 "Improved a lot" 2 "Improved somewhat" 3 "Neither improved nor gotten worse" 4 "Gotten somewhat worse" 5 "Gotten a lot worse"
label val communityEcon10yrs gotWorseLabel

rename q13a haveMoved20yrs
recode haveMoved20yrs (1=1) (2=0)

rename q13b movedOutsideArea
recode movedOutsideArea (1=1) (2=0) (9=0)

rename profile_gor region
label def regionLabel 1 "North East" 2 "North West" 3 "Yorkshire and the Humber" 4 "East Midlands" 5 "West Midlands" 6 "East of England" 7 "London" 8 "South East" 9 "South West" 10 "Wales" 11 "Scotland" 12 "Northern Ireland" 13 "Non UK & Invalid"
label val region regionLabel

//Need to generate outward postcode
gen pc_area = substr(pcode, 1, 2)
gen len1 = cond(uisdigit(substr(pc_area,2,1)), 1, 0) if pc_area != ""
replace pc_area = substr(pc_area, 1, 1) if len1 == 1

gen postcode_outward = pc_area + string(q90_digit)

sort postcode_outward

rename pcode postcode
sort postcode

//Also try merging by postcode_outward:
* First fix a few erroneous pc:
replace postcode_outward = "EC1N" if postcode_outward == "EC1"
replace postcode_outward = "G43" if postcode_outward == "G44" | postcode_outward == "G45" | postcode_outward == "GJ22" //These are in Glasgow
replace postcode_outward = "M41" if postcode_outward == "M42"
replace postcode_outward = "SW10" if postcode_outward == "SW1"  //These are in London
replace postcode_outward = "W1B" if postcode == "W1B 3HH"
replace postcode_outward = "W1H" if postcode == "W1H 4HE"
replace postcode_outward = "WC1H" if postcode == "WC1H 0NG"
replace postcode_outward = "WC2H" if postcode == "WC2H 9NJ"

//We merge in TTWAs by postcode, weighted by proportion:
*NOTE:  This bridge file is generated by "Reshape postcode to TTWA bridge.do"
sort postcode_outward
merge m:1 postcode_outward using "Regional data\Postcode to TTWA1991 (cleaned).dta"
* There are no matches for 57 respondents from NI
drop if _merge == 2
drop _merge

//Want to merge on all potential TTWAs
global shockYears "1991_2000 1991_2007 1991_2015 2000_2007 2000_2015"

forval x = 1/5 {
	rename TTWA1991_code`x' ttwa_num
	sort ttwa_num
	merge m:1 ttwa_num using "Regional data\IPW (combined years).dta"
	drop if _merge == 2
	drop _merge

	foreach yr of global shockYears {
		gen TTWA`x'_ipw_`yr' = ipw_`yr'
		gen TTWA`x'_ipwUS_`yr' = ipwUS_`yr'
	}
	rename ttwa_num TTWA1991_code`x'
	drop ipw*
}

gen noShockData = cond(TTWA1991_code1 == ., 1, 0)
forval x = 1/5 {
	foreach yr of global shockYears {
		replace TTWA`x'_ipw_`yr' = 0 if TTWA`x'_ipw_`yr' == . & noShockData == 0
		replace TTWA`x'_ipwUS_`yr' = 0 if TTWA`x'_ipwUS_`yr' == . & noShockData == 0
	}
	replace weight_postcodeoutward_TTWA1991`x' = 0 if weight_postcodeoutward_TTWA1991`x' == . & noShockData == 0
}

//Can generate "weighted" value of each measure
foreach yr of global shockYears {
	gen ipw_`yr'_weighted = TTWA1_ipw_`yr'*weight_postcodeoutward_TTWA19911 + TTWA2_ipw_`yr'*weight_postcodeoutward_TTWA19912 + TTWA3_ipw_`yr'*weight_postcodeoutward_TTWA19913 + TTWA4_ipw_`yr'*weight_postcodeoutward_TTWA19914 + TTWA5_ipw_`yr'*weight_postcodeoutward_TTWA19915
	gen ipwUS_`yr'_weighted = TTWA1_ipwUS_`yr'*weight_postcodeoutward_TTWA19911 + TTWA2_ipwUS_`yr'*weight_postcodeoutward_TTWA19912 + TTWA3_ipwUS_`yr'*weight_postcodeoutward_TTWA19913 + TTWA4_ipwUS_`yr'*weight_postcodeoutward_TTWA19914 + TTWA5_ipwUS_`yr'*weight_postcodeoutward_TTWA19915
}

drop TTWA1_* TTWA2_* TTWA3_* TTWA4_* TTWA5_*

//We define primary TTWA as first in list:
gen ttwa_num = TTWA1991_code1

//To facilitate interpretation, we rescale by dividing by 1000
replace ipw_1991_2007 = ipw_1991_2007 / 1000
replace ipwUS_1991_2007 = ipwUS_1991_2007 / 1000
replace ipw_1991_2015 = ipw_1991_2015 / 1000
replace ipwUS_1991_2015 = ipwUS_1991_2015 / 1000
replace ipw_1991_2000 = ipw_1991_2000 / 1000
replace ipwUS_1991_2000 = ipwUS_1991_2000 / 1000

replace ipw_1991_2007_weighted = ipw_1991_2007_weighted / 1000
replace ipwUS_1991_2007_weighted = ipwUS_1991_2007_weighted / 1000
replace ipw_1991_2015_weighted = ipw_1991_2015_weighted / 1000
replace ipwUS_1991_2015_weighted = ipwUS_1991_2015_weighted / 1000
replace ipw_1991_2000_weighted = ipw_1991_2000_weighted / 1000
replace ipwUS_1991_2000_weighted = ipwUS_1991_2000_weighted / 1000

label var ipw_1991_2007 "$\Delta$ IPW (1991-2007)"
label var ipwUS_1991_2007 "$\Delta$ IPW (US, 1991-2007)"
label var ipw_1991_2015 "$\Delta$ IPW (1991-2015)"
label var ipwUS_1991_2015 "$\Delta$ IPW (US, 1991-2015)"
label var ipw_1991_2000 "$\Delta$ IPW (1991-2000)"
label var ipwUS_1991_2000 "$\Delta$ IPW (US, 1991-2000)"
label var female "Female"
label var age3150 "Age (31-50)"
label var age5170 "Age (51-70)"
label var agegt70 "Age (>70)"
label var numChildren "Num. Children"
label var isMarried "Married"
label var higherCert "Higher Cert."
label var universityEduc "University"
label var rightwing "Right Ideology"
label var authoritarianTotal "Authoritarianism (total)"
label var labourVoter "Voted Labour"
label var libdemVoter "Voted Lib. Dem."
label var conservativeVoter "Voted Conservative"
label var ukipVoter "Voted UKIP"
label var ownIncLower "Lower income"
label var ownIncUpper "Upper income"

//We now save "individual-level" dataset:
save "UK full survey (indiv level).dta", replace

disp "RUN NEXT:  Merge survey with contextual data"
